require.config(window.jshead);
require(['jquery', 'pages', 'common', 'navigator'], function ($, pg, Common) {
    /*获取输入框的值*/
    function getValue() {
        var value = [];
        var itemAmount = $('#amount').val();
        var itemRateMin = $('#interestMin').val();
        var itemRateMax = $('#interestMax').val();
        var itemDayMin = $('#itemDayMin').val();
        var itemDayMax = $('#itemDayMax').val();
        value.push(itemAmount, itemRateMin, itemRateMax, itemDayMin, itemDayMax);
        return value
    }

    /*将读取出来的数据塞到页面中*/
    function setValue1(itemAmount, itemRateMin, itemRateMax, itemDayMin, itemDayMax, orderby, orderbymoney) {
        $('#amount').val(itemAmount);
        $('#interestMin').val(itemRateMin);
        $('#interestMax').val(itemRateMax);
        $('#itemDayMin').val(itemDayMin);
        $('#itemDayMax').val(itemDayMax);
    }
    /*校验数据*/
    function minMax() {
        var flag = false;
        amountFloat = parseFloat($('#amount').val());
        if (amountFloat < 100 && amountFloat!=0) {
            layer.tips('金额不能小于100元', '#amount');
            if(amountFloat==0 || isNaN(amountFloat)){
                $('#amount').val('99999999999');
                flag=false;
            }
            flag = true;
        }
        if (interestMin != '0 ' && interestMax != '99') {
            if (parseFloat(interestMin) >= parseFloat(interestMax)) {
                layer.tips('预期年化范围不正确', '#interestMax');
                flag = true;
            }
        }
        if (itemDayMin != '0' && itemDayMax != '999') {
            if (parseFloat(itemDayMin) >= parseFloat(itemDayMax)) {
                layer.tips('投资期限范围不正确', '#itemDayMax');
                flag = true;
            }
        }
        return flag;
    }
    /*输入框校验每一个数据*/
    var itemLockCycle =$('#itemLockCycle').val();
    $('#itemLockCycle').change(function () {
        itemLockCycle = $('#itemLockCycle').find("option:selected").val();
    });
    var interestMin = $('#interestMin').val();
    $('#interestMin').change(function () {
        interestMin = $('#interestMin').find("option:selected").val();
        minMax()
    });
    var interestMax = $('#interestMax').val();
    $('#interestMax').change(function () {
        interestMax = $('#interestMax').find("option:selected").val();
        minMax()
    });

    var itemDayMin = $('#itemDayMin').val();
    $('#itemDayMin').change(function () {
        itemDayMin = $('#itemDayMin').find("option:selected").val();
        minMax()
    });
    var itemDayMax = $('#itemDayMax').val();
    $('#itemDayMax').change(function () {
        itemDayMax = $('#itemDayMax').find("option:selected").val();
        minMax()
    });
    /*点击取消将输入框的值变为空*/
    $('.delete').bind('click', function () {
        window.location.href = '/user/auto?1?2'
    })
    /*点击取消关闭弹窗*/
    $('#cancel').bind('click', function () {
        $('.rebox').hide(100);
        $('#payPassword_rsainput').val('');
        $(".guangbiao").css({"left":0});
        $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
    })
    /*像后台发送数据的方法*/
    function getAuo() {
        var layerLoad = layer.load(1);
            $('#determine').attr({disabled:'disabled'});
            var VerificationCode = $('#payPassword_rsainput').val();
            if (VerificationCode == '') {
                layer.tips('交易密码不能为空', '#payPassword_rsainput');
            } else {
                var arr = getValue()
                var data = {
                    transactionPassword: VerificationCode,
                    itemAmount: arr[0],
                    itemRateMin: arr[1],
                    itemRateMax: arr[2],
                    itemDayMin: arr[3],
                    itemDayMax: arr[4],
                    itemStatus: 1,
                };
                $.ajax({
                    type: 'POST',
                    url: '/user/autoAdd',
                    dataType: "json",
                    data: data,
                    success: function (data) {
                        layer.close(layerLoad);
                        $('#determine').removeAttr('disabled');
                        if (data.result == 'fail') {
                            layer.tips(data.messageText, '#payPassword_rsainput');
                            $('#determine').removeAttr('disabled');
                            $('#payPassword_rsainput').val('').focus();
                            $(".guangbiao").css({"left":0});
                            $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
                        } else {
                            layer.msg('设置成功');
                            $('#determine').attr({disabled:'disabled'});
                            function jump() {
                                window.location.href = '/user/auto?1?2'
                                $('#payPassword_rsainput').val('');
                            }
                            window.setTimeout(jump, 1500);
                        }
                    }
                })
            }
    }
    $('#determine').bind('click', function (){
        getAuo();
    });
    //支付弹框
    $("#payPassword_rsainput").bind("blur", function (){
        $(".sixDigitPassword span").removeClass("guangbiao");
    });
    $("#payPassword_rsainput").bind("focus", function (){
        $(".sixDigitPassword span").addClass("guangbiao");
        if($(".i-text").val().length==6){

        }else{
            $(".guangbiao").css({"left":($(".i-text").val().length)*50});
        }
    });
    $(".i-text").keyup(function()
    {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用
        for( var x = 0; x<=6; x++)
        {
            //$("p").html( inp_l );//测试
            $(".sixDigitPassword").find("i").eq( inp_l ).prevAll("i").find("b").css({"display":"block"});
            $(".sixDigitPassword").find("i").eq( inp_l - 1 ).nextAll("i").find("b").css({"display":"none"});
            $(".guangbiao").css({"left":inp_l * 50});//光标位置
            if( inp_l == 0)
            {
                $(".sixDigitPassword").find("b").css({"display":"none"});
                $(".guangbiao").css({"left":0});
            }
            else if( inp_l == 6)
            {
                $(".sixDigitPassword").find("b").css({"display":"block"});
                $(".guangbiao").css({"left":5 * 50});
            }
        }
    });
    //支付页回车事件
    $(".rebox").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#determine').click();
        }
    });
    //自动投标数据
    $.ajax({
        type: "POST",
        url: "/user/autoInfo",
        dataType: "json",
        data: 'data',
        success: function (data) {
            if (data.itemAuto == null) {
                $('#automatic2').show();
                $('#automatic').hide();
                /*如果是关闭的话点击开启*/
                $('#close_open').bind('click', function () {
                    $('.order_number').html(data.autoSort+"位");
                    $('.order_number1').html(data.investAmount+"元");
                    $('#automatic2').hide();
                    $('#modify').show();
                    $('#addButton').bind('click', function () {
                        var valueData = getValue()
                        var result = minMax();
                        if (!result && $("#zidongtoubiao").is(':checked')==true){
                            $('.rebox').show(100);
                        } else if($("#zidongtoubiao").is(':checked')!=true){
                                 layer.tips('是否同意自动投标规则','#zidongtoubiaoxieyi')
                        }
                        $("#payPassword_rsainput").focus();
                    })
                })
            } else {
                if (data.itemAuto.itemStatus == 0){
                    $('#automatic').hide();
                    $('#automatic2').show();
                    /*点击开启*/
                    $('#close_open').bind('click', function () {
                        $('#automatic2').hide();
                        $('#modify').show();
                        $('.order_number').html(data.autoSort+"位");
                        $('.order_number1').html(data.investAmount+"元");
                        setValue1(data.itemAuto.itemAmount, data.itemAuto.itemRateMin, data.itemAuto.itemRateMax, data.itemAuto.itemDayMin, data.itemAuto.itemDayMax);
                        $('#addButton').bind('click', function () {
                            if($('#zidongtoubiao').is(':checked')){
                                var valueData = getValue()
                                var result = minMax()
                                if (!result) {
                                    $('.rebox').show(100);

                                } else {
                                }
                            }else{
                                layer.tips('是否同意自动投标规则','#zidongtoubiaoxieyi')
                            }

                            $("#payPassword_rsainput").focus();
                        })
                    })
                } else {
                    $('#automatic2').hide();
                    $('#automatic').show();
                    //点击关闭按钮就关闭自动投标
                    $('.close11').bind('click', function () {
                        var id = $('.close11').attr('id')
                        $.ajax({
                            type: "POST",
                            url: "/user/modifyStatus",
                            dataType: "json",
                            data: {
                                id: id,
                            },
                            success: function (data) {
                                if (data.result == 'success') {
                                    layer.msg('关闭投标');
                                    function jump() {
                                        window.location.href = '/user/auto?1?2'
                                    }
                                    window.setTimeout(jump, 1500);
                                } else {
                                    layer.alert('请重新关闭');
                                    function jump() {
                                        window.location.href = '/user/auto?1?2'
                                    }
                                    window.setTimeout(jump, 1500);
                                }
                            }
                        })
                    })
                    $('.orderBy2Money').html(data.investAmount+"元");
                    $('.orderBy2').html(data.autoSort+"位");
                    if(data.itemAuto.itemRateMax===99){
                        $('.interestMin2').html('无限制');
                    }else{
                        $('.interestMin2').html(data.itemAuto.itemRateMax + '%');
                    }
                    if(data.itemAuto.itemDayMax===999){
                        $('.itemDayMin2').html('无限制');
                    }else{
                        $('.itemDayMin2').html(data.itemAuto.itemDayMax + '天');
                    }

                    if(data.itemAuto.itemRateMin===0){
                        $('.interestMin1').html('无限制');
                    }else{
                        $('.interestMin1').html(data.itemAuto.itemRateMin + '%');
                    }
                    if(data.itemAuto.itemDayMin===0){
                        $('.itemDayMin1').html('无限制');
                    }else{
                        $('.itemDayMin1').html(data.itemAuto.itemDayMin + '天');
                    }
                    if(data.itemAuto.itemAmount==0 || data.itemAuto.itemAmount==""){
                        $(".total1_money").html('无限制');
                    }else{
                        $(".total1_money").html(data.itemAuto.itemAmount);
                    }

                    $('.close11').attr('id', data.itemAuto.id);
                    $('#modify1').bind('click', function () {
                        $('#automatic').hide();
                        $('#modify').show();
                        $('.order_number').html(data.autoSort+"位");
                        $('.order_number1').html(data.investAmount+"元");
                        setValue1(data.itemAuto.itemAmount, data.itemAuto.itemRateMin, data.itemAuto.itemRateMax, data.itemAuto.itemDayMin, data.itemAuto.itemDayMax)
                        $('#addButton').bind('click', function () {
                            var valueData = getValue()
                            var result = minMax()
                            if (!result) {
                                $('.rebox').show(100);
                            } else {
                            }
                            $("#payPassword_rsainput").focus();
                        })
                    })
                }
            }
        }
    });
    /*实名认证用的*/
    $.ajax({
        type: "POST",
        url: "/user/security/information",
        dataType: "json",
        success: function (data) {

            if (data.security.identifyCard == null) {
                layer.alert('未绑定实名认证，即将跳转至实名认证页面');
                function jump() {
                    window.location.href = '/user/security/realname'
                }

                window.setTimeout(jump, 1500);
            } else {
                $.ajax({
                    type: "POST",
                    url: "/account/bank/getBankByUserId",
                    dataType: "json",
                    success: function (data) {
                        if (data.result == 'fail') {
                            layer.alert('未绑定银行卡，即将跳转至银行卡页面');
                            function jump() {
                                window.location.href = '/user/security/realname'
                            }

                            window.setTimeout(jump, 1500);
                        }
                    }
                });
            }
        }
    });

    $.ajax({
        type:'post',
        url:'/user/info/isSignContract',
        dataType:'json',
        success:function(data){
            if(data.isItemAuto==0){
                $('.openxieyi a').prop('href','/html/registerxy1.html');
                $(".openxieyi input[type='checkbox']").removeAttr('disabled')
            }else{
                $(".openxieyi input[type='checkbox']").prop("checked",'true');
                $(".openxieyi input[type='checkbox']").prop('disabled','true');
                $('.openxieyi a').prop('href',data.url);
            }
        }
    })
  /*  $('#addButton').bind('click',function(){
        console.log($('#zidongtoubiao').attr("checked")==true)
            if($('#zidongtoubiao').is(':checked')){
                 alert('nihao')
            }else{
                $('#addButton').attr('disabled')
            }
    })*/

  //获取到页面上的数据并且做修改
    setTimeout(function(){
        $('.bid_info').show()
    },300)
});
